package au.com.iglootech.TaxTimeToolTip.ui.fillupform;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Scope;

import au.com.iglootech.TaxTimeToolTip.Entity.Company;
import au.com.iglootech.TaxTimeToolTip.Entity.FeeRate;
import au.com.iglootech.TaxTimeToolTip.dao.GenericDao;
import au.com.iglootech.TaxTimeToolTip.service.CompanyService;
import au.com.iglootech.TaxTimeToolTip.style.TaxTimeToolTipStyle;

import com.vaadin.data.Item;
import com.vaadin.data.util.BeanContainer;
import com.vaadin.data.validator.IntegerValidator;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DateField;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;

@Configurable(preConstruction = true)
@org.springframework.stereotype.Component
@Scope("request")
public class TimeSheetFieldsFactory extends DefaultFieldFactory {
	@Autowired
	private transient CompanyService companyService;
	@Autowired
	private transient GenericDao genericDao;

	@Override
	public Field createField(Item item, Object propertyId, Component uiContext) {
		Field f;

		// Use the super class to create a suitable field base on the
		// property type.
		if ("company".equals(propertyId)) {

			BeanContainer<String, Company> beans = new BeanContainer<String, Company>(
					Company.class);

			// Use the name property as the item ID of the bean
			List<Company> companies = companyService.findAllCompany();
			beans.setBeanIdProperty("key");
			ComboBox companyComBox = new ComboBox("Company");
			companyComBox.setWidth(TaxTimeToolTipStyle.COMMON_FIELD_WIDTH);
			companyComBox.setImmediate(true);
			companyComBox.setInvalidAllowed(false);
			companyComBox.setNullSelectionAllowed(false);
			for (Company company : companies) {
				beans.addBean(company);
				companyComBox.addItem(company);
				companyComBox.setItemCaption(company, company.getCompanyName());
			}

			return companyComBox;
		} else if ("unit".equals(propertyId)) {
			ComboBox unitComBox = new ComboBox("Unit (AUS)");
			unitComBox.setWidth(TaxTimeToolTipStyle.COMMON_FIELD_WIDTH);
			unitComBox.setInvalidAllowed(false);
			unitComBox.setNullSelectionAllowed(false);
			List<FeeRate> feeRates = new ArrayList<FeeRate>(genericDao.findAll(FeeRate.class));
			for(FeeRate rate : feeRates)
			{
				unitComBox.addItem(rate.getRate());
			}
			
			return unitComBox;
		} else if ("description".equals(propertyId)) {
			TextArea tf = new TextArea("description");
			tf.setRequiredError("Please enter description");
			tf.setNullRepresentation("");
			tf.setWidth(TaxTimeToolTipStyle.COMMON_FIELD_WIDTH);
			return tf;
		}
		else {
			f = super.createField(item, propertyId, uiContext);
		}
		if ("taskName".equals(propertyId)) {
			TextField tf = (TextField) f;
			tf.setRequired(true);
			tf.setRequiredError("Please enter task name");
			tf.setNullRepresentation("");
			tf.setWidth(TaxTimeToolTipStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new StringLengthValidator(
					"First Name must be 3-25 characters", 3, 25, false));
		} else if ("period".equals(propertyId)) {
			TextField tf = (TextField) f;
			tf.setRequired(true);
			tf.setNullRepresentation("");
			tf.setNullSettingAllowed(false);
			tf.setWidth(TaxTimeToolTipStyle.COMMON_FIELD_WIDTH);
			tf.addValidator(new IntegerValidator("Shoe size must be an Integer"));

		} else if ("startDate".equals(propertyId)) {
			DateField tf = (DateField) f;
			tf.setDateFormat("dd/MM/yyyy");
			tf.setRequired(true);
			tf.setRequiredError("Please enter a Last Name");
			tf.setWidth(TaxTimeToolTipStyle.COMMON_FIELD_WIDTH);
		} 
		return f;
	}
}